<?php

class Act_agents extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('magents');
        $this->load->library('session');
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->load->model('mmaster');
        $this->load->model('mglobal');
	}

    ########################################################################
    ########################################################################

    public function getById($id) {
        if (isset($id))
            echo json_encode($this->mmaster->getAgents($id));
    }
    
    public function read( $kindOfRecord ) {
		//echo json_encode( $this->magents->getAgents( $kindOfRecord ) );
		
		//encode
		$result = $this->magents->getAgents( $kindOfRecord );

        foreach ($result['aaData'] as $r => &$value) 
        {
            $value[0] = $this->enc->fogit($value[0]);
        }
        
        //penambahan nomor statis untuk grid SSP
        $start=$_GET['iDisplayStart'];
        $result['iDisplayStart']=$start;
        echo json_encode($result);
	}

	function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }

	
	public function update( $kindOfRecord, $id )
	{
		$this->magents->updateAgent( $id );
	}
	
	public function delete( $table_type, $id = null ) {
		//decode id
    	$id = $this->enc->defog($id); 
		
		if( is_null( $id ) ) {
			echo 'ERROR: Id not provided.';
			return;
		}
		
		$this->magents->deleteAgents( $table_type, $id );
		if( $table_type == 'temp' )
			$this->session->set_flashdata('message', 'Perubahan data ditolak');
		else
			$this->session->set_flashdata('message', 'Data berhasil dihapus');
		
		redirect(base_url()."index.php/master/agents");
	}

    ########################################################################
    ########################################################################

    public function getStatusAgentsByIdJson($userId, $status) {
        $data = $this->magents->selectStatusAgentsById($userId, $status);
        for ($i = 0; $i < sizeof($data); $i++) {
            if ($status == 0) {
                $data[$i]->actions = "<a id=\"dialog_link\" href=\"" . base_url("index.php/agents/approval/update_agent_status_new:" . $data[$i]->agent_id) . "\">Approve</a> <a href=\"\">Map</a> <a href=\"\">View Photos</a>";
                //$data[$i]->actions = "<a id=dialog_link href=\"#\">Approve</a> <a href=\"\">Map</a> <a href=\"\">View Photos</a>";
            } else if ($status == 2) {
                $data[$i]->actions = "<a id=\"dialog_link\" href=\"" . base_url("index.php/agents/approval/update_agent_status_edited:" . $data[$i]->agent_id) . "\">Approve</a> <a href=\"\">Map</a> <a href=\"\">View Photos</a>";
            } else {
                // $data[$i]->actions = "No Actions Available";
                $data[$i]->actions = "<a id=\"dialog_link\" href=\"" . base_url("index.php/agents/approval/update_agent_status_registered:" . $data[$i]->agent_id) . "\">Unapprove</a> <a href=\"\">Map</a> <a href=\"\">View Photos</a>";
            }
        }
        echo json_encode($data);
    }
    
    public function view($param){

    	//decode id
    	$param = $this->enc->defog($param); 

        $data['template'] = "shell/smooth";
        $data['main_view'] = 'f-view-agents';
        $result = $this->magents->getAgents( "", $param );
   	    $pic = $this->magents->getPIC( $param );
		$arr = array( 'a_status', 'identity', 'o_location', 'a_type', 'b_focus' );
		$fillData = $this->fillData( $arr, 'direct sales' );
	    $data = array_merge( $data, $fillData );
        //print_r($data['data']['maintain_name']);
        
			if( $result != null )
			{
				$data[ 'agent_name' ] = $result->agent_name;
				$data[ 'agent_status' ] = $result->agent_status;
                $data[ 'agent_selling_type' ] = $result->agent_selling_type;
				$data[ 'agent_address' ] = $result->agent_address;
				$data[ 'agent_city' ] = $result->agent_city;
				$data[ 'agent_province' ] = $result->agent_province_id;
				$data[ 'agent_id' ] = $result->agent_id;
				$data[ 'agent_zip_code' ] = $result->agent_zip_code;
				$data[ 'agent_phone_no' ] = $result->agent_phone_no;
				$data[ 'agent_mdn_evo' ] = $result->agent_mdn_evo; 
				$data[ 'maintain_name' ] = $this->mmaster->getMaintainName( $result->territory_id , 'direct sales'); 
				$data[ 'agent_website' ] = $result->agent_website; 
				$data[ 'is_school' ] = $result->is_agent_a_school; 
				$data[ 'image' ] = $result->image_path;
				
			/**
             * 	if( $data[ 'is_school' ] == true )
             * 				{
             * 					
             * 					$school = $this->magents->getAgentSchool( $param2 );
             * 					$data[ 'majority_num' ] = $school->school_number_of_majority;
             * 					$data[ 'faculty_num' ] = $school->school_number_of_faculty;
             * 					$data[ 'student_num' ] = $school->school_number_of_student;
             * 					$data[ 'teacher_num' ] = $school->school_number_of_teacher;
             * 				}
             * 				
             */
				$data[ 'employee_num' ] = $result->agent_number_of_employee; 
				$data[ 'agent_location' ] = $result->agent_location; 
				$data[ 'agent_type' ] = $result->agent_type; 
				$data[ 'agent_business_focus' ] = $result->agent_business_focus; 
			}
			
			if( $pic != null )
			{
				$data[ 'pic_name' ] = $pic->pic_name;
				$data[ 'pic_phone' ] = $pic->pic_phone_no;
				$data[ 'pic_address' ] = $pic->pic_address;
				$data[ 'pic_city' ] = $pic->pic_city;
				$data[ 'pic_province' ] = $pic->pic_province_id;
				$data[ 'pic_zip_code' ] = $pic->pic_zip_code;
				$data[ 'pic_identity_type' ] = $pic->pic_identity_type;
				$data[ 'pic_identity_no' ] = $pic->pic_identity_no;
				$data[ 'pic_birth_place' ] = $pic->pic_birth_place;
				$data[ 'pic_birth_date' ] = $pic->pic_birth_date;
				$data[ 'pic_job_position' ] = $pic->pic_job_position;
				$data[ 'pic_smartfren_no' ] = $pic->pic_smartfren_no;
				$data[ 'pic_mobile_no' ] = $pic->pic_mobile_no;
				$data[ 'pic_email' ] = $pic->pic_email;
			}
            
		$this->load->view($data['template'],$data);
    }
    
    	private function fillData( $arr, $type )
	{		
		foreach( $arr as $combo )
		{
			$query = $this->mmaster->getComboData( $combo );
			$i = 0;
			foreach( $query->result() as $r )
			{
				$data[ 'data' ][ $combo ][ 'member_value' ][ $i ] = $r->member_value;
				$data[ 'data' ][ $combo ][ 'member_display' ][ $i ] = $r->member_display;
				$i++;
			}
		}
		
		/**************************** ISI COMBO PROVINCE *****************************/
		$query = $this->mmaster->getRecord( 't_mtr_province' );
		$i = 0;
		foreach( $query->result() as $r )
		{
			$data[ 'data' ][ 'province' ][ 'member_value' ][ $i ] = $r->province_id;
			$data[ 'data' ][ 'province' ][ 'member_display' ][ $i ] = $r->province_name;
			$i++;
		}
				
		/*******************************ISI TERRITORY PATH******************************/
		$result = $this->mmaster->getTerritoryPath( $this->session->userdata( 'username' ), 'cluster' );
		$data[ 'data' ][ 'tr_path' ][ 'member_display' ] = $result[ 'path' ];
		$data[ 'data' ][ 'tr_path' ][ 'member_value' ] = $result[ 'territory_id' ];
		
		
		/************* ISI COMBO FIELD NAME PADA MAINTAINED BY *************************/

  /**
 * 	$query = $this->mmaster->getMaintainName( '', $type );
 *   		$i = 0;
 *   		if( $query != null )
 *   		{
 *   			foreach( $query->result() as $r )
 *   			{
 *   				$data[ 'data' ][ 'maintain_name' ][ 'member_display' ][ $i ] = $r->user_name;
 *   				$data[ 'data' ][ 'maintain_name' ][ 'member_value' ][ $i ] = $r->user_id;
 *   				$data[ 'data' ][ 'maintain_name' ][ 'pos' ][ $i ] = $this->mmaster->getPosition( $r->user_id )->row()->user_group_name;
 *   				$i++;
 *   			}
 *   		}
 *   		else
 *   		{
 *   			$data[ 'data' ][ 'maintain_name' ][ 'member_display' ][ $i ] = '';
 *   			$data[ 'data' ][ 'maintain_name' ][ 'member_value' ][ $i ] = '';
 *   			$data[ 'data' ][ 'maintain_name' ][ 'pos' ][ $i ] = '';				
 *   		}
 */
 
		return $data;
	}
    
     public function viewDiff($param){
        
    	$param = $this->enc->defog($param); 
        $data['template'] = "shell/smooth";
        $data['main_view'] = 'f-view-diff-agents';
        $result = $this->magents->getAgents( "", $param );
        $result2 = $this->magents->getUnapprovedAgents( $param );
   	    $pic = $this->magents->getPIC( $param );
        $pic2 = $this->magents->getUnapprovedPIC( $param );
		$arr = array( 'a_status', 'identity', 'o_location', 'a_type', 'b_focus' );
		$fillData = $this->fillData( $arr, 'direct sales' );
	    $data = array_merge( $data, $fillData );

			
			$data[ 'agent_name' ] = $result2->agent_name;
			$data[ 'agent_status' ] = $result2->agent_status;
            $data[ 'agent_selling_type' ] = $result2->agent_selling_type;
			$data[ 'agent_address' ] = $result2->agent_address;
			$data[ 'agent_city' ] = $result2->agent_city;
			$data[ 'agent_province' ] = $result2->agent_province_id;
			$data[ 'agent_id' ] = $result2->agent_id;
			$data[ 'agent_zip_code' ] = $result2->agent_zip_code;
			$data[ 'agent_phone_no' ] = $result2->agent_phone_no;
			$data[ 'agent_mdn_evo' ] = $result2->agent_mdn_evo; 
			$data[ 'maintain_name' ] = $this->mmaster->getUnapprovedMaintainName( $result2->territory_id , 'direct sales'); 
			$data[ 'agent_website' ] = $result2->agent_website; 
			$data[ 'is_school' ] = $result2->is_agent_a_school; 
			$data[ 'image' ] = $result2->image_path;
				
			/**
             * 	if( $data[ 'is_school' ] == true )
             * 				{
             * 					
             * 					$school = $this->magents->getAgentSchool( $param2 );
             * 					$data[ 'majority_num' ] = $school->school_number_of_majority;
             * 					$data[ 'faculty_num' ] = $school->school_number_of_faculty;
             * 					$data[ 'student_num' ] = $school->school_number_of_student;
             * 					$data[ 'teacher_num' ] = $school->school_number_of_teacher;
             * 				}
             * 				
             */
			$data[ 'employee_num' ] = $result2->agent_number_of_employee; 
			$data[ 'agent_location' ] = $result2->agent_location; 
			$data[ 'agent_type' ] = $result2->agent_type; 
			$data[ 'agent_business_focus' ] = $result2->agent_business_focus; 
            
            $data['old'][ 'agent_name' ] = $result->agent_name;
			$data['old'][ 'agent_status' ] = $result->agent_status;
            $data['old'][ 'agent_selling_type' ] = $result->agent_selling_type;
			$data['old'][ 'agent_address' ] = $result->agent_address;
			$data['old'][ 'agent_city' ] = $result->agent_city;
			$data['old'][ 'agent_province' ] = $result->agent_province_id;
			$data['old'][ 'agent_id' ] = $result->agent_id;
			$data['old'][ 'agent_zip_code' ] = $result->agent_zip_code;
			$data['old'][ 'agent_phone_no' ] = $result->agent_phone_no;
			$data['old'][ 'agent_mdn_evo' ] = $result->agent_mdn_evo; 
			$data['old'][ 'maintain_name' ] = $this->mmaster->getMaintainName( $result->territory_id , 'direct sales'); 
			$data['old'][ 'agent_website' ] = $result->agent_website; 
			$data['old'][ 'is_school' ] = $result->is_agent_a_school; 
			$data['old'][ 'image' ] = $result->image_path;
				
			/**
             * 	if( $data[ 'is_school' ] == true )
             * 				{
             * 					
             * 					$school = $this->magents->getAgentSchool( $param2 );
             * 					$data[ 'majority_num' ] = $school->school_number_of_majority;
             * 					$data[ 'faculty_num' ] = $school->school_number_of_faculty;
             * 					$data[ 'student_num' ] = $school->school_number_of_student;
             * 					$data[ 'teacher_num' ] = $school->school_number_of_teacher;
             * 				}
             * 				
             */
			$data['old'][ 'employee_num' ] = $result->agent_number_of_employee; 
			$data['old'][ 'agent_location' ] = $result->agent_location; 
			$data['old'][ 'agent_type' ] = $result->agent_type; 
			$data['old'][ 'agent_business_focus' ] = $result->agent_business_focus; 
            
			
			
			
			$data[ 'pic_name' ] = $pic2->pic_name;
			$data[ 'pic_phone' ] = $pic2->pic_phone_no;
			$data[ 'pic_address' ] = $pic2->pic_address;
			$data[ 'pic_city' ] = $pic2->pic_city;
			$data[ 'pic_province' ] = $pic2->pic_province_id;
			$data[ 'pic_zip_code' ] = $pic2->pic_zip_code;
			$data[ 'pic_identity_type' ] = $pic2->pic_identity_type;
			$data[ 'pic_identity_no' ] = $pic2->pic_identity_no;
			$data[ 'pic_birth_place' ] = $pic2->pic_birth_place;
			$data[ 'pic_birth_date' ] = $pic2->pic_birth_date;
			$data[ 'pic_job_position' ] = $pic2->pic_job_position;
			$data[ 'pic_smartfren_no' ] = $pic2->pic_smartfren_no;
			$data[ 'pic_mobile_no' ] = $pic2->pic_mobile_no;
			$data[ 'pic_email' ] = $pic2->pic_email;
            
           	$data['old'][ 'pic_name' ] = $pic->pic_name;
			$data['old'][ 'pic_phone' ] = $pic->pic_phone_no;
			$data['old'][ 'pic_address' ] = $pic->pic_address;
			$data['old'][ 'pic_city' ] = $pic->pic_city;
			$data['old'][ 'pic_province' ] = $pic->pic_province_id;
			$data['old'][ 'pic_zip_code' ] = $pic->pic_zip_code;
			$data['old'][ 'pic_identity_type' ] = $pic->pic_identity_type;
			$data['old'][ 'pic_identity_no' ] = $pic->pic_identity_no;
			$data['old'][ 'pic_birth_place' ] = $pic->pic_birth_place;
			$data['old'][ 'pic_birth_date' ] = $pic->pic_birth_date;
			$data['old'][ 'pic_job_position' ] = $pic->pic_job_position;
			$data['old'][ 'pic_smartfren_no' ] = $pic->pic_smartfren_no;
			$data['old'][ 'pic_mobile_no' ] = $pic->pic_mobile_no;
			$data['old'][ 'pic_email' ] = $pic->pic_email;
            
			
            
		$this->load->view($data['template'],$data);
    }
    
    

}